//
//  StoresGoodsTableCollectionViewCell.m
//  TerritoryShopkeeper
//
//  Created by 李小龙 on 2017/12/19.
//  Copyright © 2017年 ZDQK. All rights reserved.
//

#import "StoresGoodsTableCollectionViewCell.h"
#import "StoresGoodsTableViewCell.h"
#import "StoresNearbyModel.h"

@interface StoresGoodsTableCollectionViewCell ()<UITableViewDataSource, UITableViewDelegate,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>

@property (nonatomic, strong) NSArray *dataArr;

@end

@implementation StoresGoodsTableCollectionViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    [self configureUI];
}
#pragma mark - Method
static NSString *storesGoodsTableIdentifier = @"storesGoodsTableIdentifier";
- (void)configureUI {
    [self.tableView registerNib:[UINib nibWithNibName:@"StoresGoodsTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:storesGoodsTableIdentifier];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView clearExtendCellLine];
}
- (void)setTableViewData:(NSMutableArray *)tableArray {
    if (![tableArray isKindOfClass:[NSArray class]]) {
        return;
    }
    self.dataArr = [NSArray arrayWithArray:tableArray];
    [self.tableView reloadData];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    StoresGoodsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:storesGoodsTableIdentifier forIndexPath:indexPath];
    
    StoresGoodModel *model = self.dataArr[indexPath.row];
    //商品图片
    [cell.goodImageView sd_setImageWithURL:GetUrl(model.gimg) placeholderImage:PlaceholderImageSmall changeContentModel:UIViewContentModeScaleAspectFill];
    //商品名称
    [cell.goodNameLabel setText:model.gname];
    //商品现价原价
    NSString *priceStr = [NSString stringWithFormat:@"¥%@\n原价¥%@",model.gprice,model.goldprice];
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:priceStr];
    [attributedStr addAttributes:@{NSFontAttributeName:TextPingFangMediumFontSize(14),
                                   NSForegroundColorAttributeName:[UIColor ThemeColor]}
                           range:NSMakeRange(0, 1)];
    [attributedStr addAttributes:@{NSFontAttributeName:TextPingFangMediumFontSize(20),
                                   NSForegroundColorAttributeName:[UIColor ThemeColor]}
                           range:[priceStr rangeOfString:[NSString stringWithFormat:@"%@",model.gprice]]];
    [attributedStr addAttributes:@{NSFontAttributeName:TextPingFangFontSize(12),
                                   NSForegroundColorAttributeName:[UIColor grayTextColor]}
                           range:[priceStr rangeOfString:[NSString stringWithFormat:@"原价¥%@",model.goldprice]]];
    
    
    NSRange tempRange = [priceStr rangeOfString:[NSString stringWithFormat:@"原价¥%@",model.goldprice]];
    [attributedStr addAttributes:@{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle|NSUnderlinePatternSolid),
                                   NSStrikethroughColorAttributeName:[UIColor grayTextColor]}
                           range:NSMakeRange(tempRange.location+2, tempRange.length-2)];
    [cell.goodPriceLabel setAttributedText:attributedStr];
//    //加减按钮，输入框
//    if (model.count.integerValue > 0) {
////        [cell.subButton setHidden:NO];
////        [cell.countTF setHidden:NO];
//        [cell.countTF setText:model.count];
//    } else {
////        [cell.subButton setHidden:YES];
////        [cell.countTF setHidden:YES];
//        [cell.countTF setText:0];
//    }
    [cell.subButton setHidden:YES];
    [cell.addButton setHidden:YES];
    [cell.countTF setHidden:YES];
    //已选数量
    [cell.chooseButton setRedPointText:model.count];
    
    
    __block typeof(self) blockSelf = self;
    __block NSInteger countInt = 0;
    cell.block = ^(StoresGoodCellClickType type, NSString *count) {
        ClickType clickType;
        switch (type) {
            case StoresGoodCellClickTypeAdd:
            {
                clickType = ClickTypeAdd;
                //model.count = [NSString stringWithFormat:@"%ld",(long)(model.count.integerValue+1)];
                countInt = 1;
            } break;
            case StoresGoodCellClickTypeSub:
            {
                clickType = ClickTypeSub;
                //model.count = [NSString stringWithFormat:@"%ld",(long)(model.count.integerValue-1)];
                countInt = -1;
            } break;
            case StoresGoodCellClickTypeText:
            {
                clickType = ClickTypeText;
                //model.count = count;
                countInt = count.integerValue - model.count.integerValue;
            } break;
            case StoresGoodCellClickTypeChoose:
            {
                clickType = ClickTypeChoose;
                //model.count = count;
                countInt = 0;
            } break;
            default:
                break;
        }
        if (blockSelf.block) {
            blockSelf.block(clickType, countInt, indexPath.row);
        }
    };
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 95;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.block) {
        self.block(ClickTypeNormal, 0, indexPath.row);
    }
}
#pragma mark - DZNEmptyDataSetSource, DZNEmptyDataSetDelegate
//主提示标题
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    return [[NSAttributedString alloc] initWithString:@"暂无商品"
                                           attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:16.0],
                                                        NSForegroundColorAttributeName:[UIColor grayTextColor]}];
}

@end
